package hisp;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class HealthRecord {
	private final String defaultPrivateRecord = "<private><patient><name><firstname /><lastname /></name><contact><street /><city /><state /><zip /><phone /><fax /><email /></contact></patient><guardian><name><firstname /><lastname /></name><contact><street /><city /><state /><zip /><phone /><fax /><email /></contact></guardian><primarydoctor><name><firstname /><lastname /></name><contact><street /><city /><state /><zip /><phone /><fax /><email /></contact></primarydoctor></private>";
	private final String defaultPublicRecord = "<public><publicstat><height>0.0</height><weight>0.0</weight><age>0</age><gender /><race /><city /><state /></publicstat><medical><diagnosis><doctor /><diagnosis /><date /></diagnosis><treatment><type /><description /><comment /><date /></treatment><testresult><test /><result /><comment /><date /><performedby /></testresult></medical></public>";
	private Document privateRecord = null;
	private Document publicRecord = null;
	private PrivateHealthRecord privateRecordObject;
	private PublicHealthRecord publicRecordObject;
	public String patientId;
	public String identifier;

	public class Person {
		public String firstName = "";
		public String lastName = "";
		public Contact contact;

		public Person() {
			contact = new Contact();
		}
	}

	public class Contact {
		public String street = "";
		public String city = "";
		public String state = "";
		public String zip = "";
		public String phone = "";
		public String fax = "";
		public String email = "";
	}

	public class PrivateHealthRecord {
		public Person patient;
		public Person guardian;
		public Person doctor;

		public PrivateHealthRecord() {
			patient = new Person();
			guardian = new Person();
			doctor = new Person();
		}
	}

	public class PublicHealthRecord {
		public PublicStat publicStat;
		public MedicalRecord medical;

		public PublicHealthRecord() {
			publicStat = new PublicStat();
			medical = new MedicalRecord();
		}
	}

	public class PublicStat {
		public double height;
		public double weight;
		public int age;
		public String gender;
		public String race;
		public String city;
		public String state;

		public PublicStat() {
			height = 0.0;
			weight = 0.0;
			age = 0;
			gender = "";
			race = "";
			city = "";
			state = "";
		}
	}

	public class Diagnosis {
		public String date;
		public String diagnosis;

		public Diagnosis() {
			date = "";
			diagnosis = "";
		}
	}

	public class Treatment {
		public String type;
		public String description;
		public String comment;
		public String date;

		public Treatment() {
			type = "";
			description = "";
			comment = "";
			date = "";
		}
	}

	public class TestResult {
		public String test;
		public String result;
		public String comment;
		public String date;
		public String performedby;

		public TestResult() {
			test = "";
			result = "";
			comment = "";
			date = "";
			performedby = "";
		}
	}

	public class MedicalRecord {
		public Diagnosis diagnosis;
		public Treatment treatment;
		public TestResult testResult;

		public MedicalRecord() {
			diagnosis = new Diagnosis();
			treatment = new Treatment();
			testResult = new TestResult();
		}
	}

	public HealthRecord(String patientId, String identifier) {
		SAXBuilder builder = new SAXBuilder();
		try {
			privateRecord = builder.build(new ByteArrayInputStream(
					defaultPrivateRecord.getBytes()));
			publicRecord = builder.build(new ByteArrayInputStream(
					defaultPublicRecord.getBytes()));
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.patientId = patientId;
		this.identifier = identifier;

		buildRecordData();
	}

	public HealthRecord(String patientId, String identifier,
			String privateRecordXml, String publicRecordXml) {
		SAXBuilder builder = new SAXBuilder();
		try {
			privateRecord = builder.build(new ByteArrayInputStream(
					privateRecordXml.getBytes()));
			publicRecord = builder.build(new ByteArrayInputStream(
					publicRecordXml.getBytes()));
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		buildRecordData();
		this.patientId = patientId;
		this.identifier = identifier;
	}

	private void buildRecordData() {
		privateRecordObject = new PrivateHealthRecord();
		publicRecordObject = new PublicHealthRecord();
		
		Element root = privateRecord.getRootElement();
		Element patient = (Element) root.getChildren("patient").get(0);
		Element name = patient.getChild("name");
		if (name == null) System.out.println("name null");
		privateRecordObject.patient.firstName = insertString(name, "firstname");
		privateRecordObject.patient.lastName = insertString(name, "lastname");
		Element contact = patient.getChild("contact");
		privateRecordObject.patient.contact.street = insertString(contact, "street");
		privateRecordObject.patient.contact.city = insertString(contact, ("city"));
		privateRecordObject.patient.contact.state = insertString(contact ,"state");
		privateRecordObject.patient.contact.zip = insertString(contact ,"zip");
		privateRecordObject.patient.contact.phone = insertString(contact ,"phone");
		privateRecordObject.patient.contact.fax = insertString(contact ,"fax");
		privateRecordObject.patient.contact.email = insertString(contact ,"email");
		Element guardian = (Element) root.getChildren("guardian").get(0);
		name = guardian.getChild("name");
		privateRecordObject.guardian.firstName = insertString(name, "firstname");
		privateRecordObject.guardian.lastName = insertString(name, "lastname");
		contact = guardian.getChild("contact");
		privateRecordObject.guardian.contact.street = insertString(contact ,"street");
		privateRecordObject.guardian.contact.city = insertString(contact ,"city");
		privateRecordObject.guardian.contact.state = insertString(contact ,"state");
		privateRecordObject.guardian.contact.zip = insertString(contact ,"zip");
		privateRecordObject.guardian.contact.phone = insertString(contact ,"phone");
		privateRecordObject.guardian.contact.fax = insertString(contact ,"fax");
		privateRecordObject.guardian.contact.email = insertString(contact ,"email");
		Element primarydoctor = (Element) root.getChildren("primarydoctor").get(0);
		name = primarydoctor.getChild("name");
		privateRecordObject.doctor.firstName = insertString(name, "firstname");
		privateRecordObject.doctor.lastName = insertString(name, "lastname");
		contact = primarydoctor.getChild("contact");
		privateRecordObject.doctor.contact.street = insertString(contact ,"street");
		privateRecordObject.doctor.contact.city = insertString(contact ,"city");
		privateRecordObject.doctor.contact.state = insertString(contact ,"state");
		privateRecordObject.doctor.contact.zip = insertString(contact ,"zip");
		privateRecordObject.doctor.contact.phone = insertString(contact ,"phone");
		privateRecordObject.doctor.contact.fax = insertString(contact ,"fax");
		privateRecordObject.doctor.contact.email = insertString(contact ,"email");
		
		root = publicRecord.getRootElement();
		Element publicStat = (Element) root.getChildren("publicstat").get(0);
		if (publicStat == null ) System.out.println("stat is null");
		publicRecordObject.publicStat.city = insertString(publicStat ,"city");
		publicRecordObject.publicStat.state = insertString(publicStat ,"state");
		publicRecordObject.publicStat.gender = insertString(publicStat ,"gender");
		publicRecordObject.publicStat.race = insertString(publicStat ,"race");
		
		
		publicRecordObject.publicStat.weight = insertDouble(publicStat ,"weight");
		publicRecordObject.publicStat.height = insertDouble(publicStat ,"height");
		
		
		publicRecordObject.publicStat.age = insertInteger(publicStat ,"age");
		
		Element diagnosis = (Element) root.getChild("medical").getChildren("diagnosis").get(0);
		if (diagnosis == null) publicRecordObject.medical.diagnosis = null;
		else {
			publicRecordObject.medical.diagnosis.date = diagnosis.getChild("date").getText();
			publicRecordObject.medical.diagnosis.diagnosis = diagnosis.getChild("diagnosis").getText();
		}
		
		Element treatment = (Element) root.getChild("medical").getChildren("treatment").get(0);
		if (treatment == null) publicRecordObject.medical.treatment = null;
		else {
			publicRecordObject.medical.treatment.type = treatment.getChild("type").getText();
			publicRecordObject.medical.treatment.description = treatment.getChild("description").getText();
			publicRecordObject.medical.treatment.comment = treatment.getChild("comment").getText();
			publicRecordObject.medical.treatment.date = treatment.getChild("date").getText();
		}
		
		Element testResult = (Element) root.getChild("medical").getChildren("testresult").get(0);
		if (testResult == null) publicRecordObject.medical.testResult = null;
		else {
			publicRecordObject.medical.testResult.test = testResult.getChild("test").getText();
			publicRecordObject.medical.testResult.result = testResult.getChild("result").getText();
			publicRecordObject.medical.testResult.comment = testResult.getChild("comment").getText();
			publicRecordObject.medical.testResult.date = testResult.getChild("date").getText();
			publicRecordObject.medical.testResult.performedby = testResult.getChild("performedby").getText();
		}		
	}

	public String toPrivateXml() {
		XMLOutputter outputter = new XMLOutputter(Format.getRawFormat());
		return outputter.outputString(privateRecord).replaceAll("[\n\r]", "");
	}

	public String toPublicXml() {
		XMLOutputter outputter = new XMLOutputter(Format.getRawFormat());
		return outputter.outputString(publicRecord).replaceAll("[\n\r]", "");
	}

	private String insertString(Element e, String itemname) {
		if (e.getChild(itemname) == null)
			return "";
		else
			return e.getChildText(itemname);
	}
	
	private Double insertDouble(Element e, String itemname) {
		if (e.getChild(itemname) == null)
			return 0.0;
		else
			return Double.valueOf(e.getChildText(itemname));
	}
	
	private int insertInteger(Element e, String itemname) {
		if (e.getChild(itemname) == null)
			return 0;
		else
			return Integer.valueOf(e.getChildText(itemname));
	}

	public void patientUpdate(Identity identity, BufferedReader stdin) {
		Element private_root = privateRecord.getRootElement();
		Element public_root = publicRecord.getRootElement();
		Element patient = (Element) private_root.getChildren("patient").get(0);
		Element publicStat = (Element) public_root.getChildren("publicstat")
				.get(0);
		Element newPatient = new Element("patient").setAttribute("modifiedby",
				identity.id);
		Element newPublicStat = new Element("publicstat").setAttribute(
				"modified", identity.id);

		String input;
		boolean changed = false;

		try {
			Element name = new Element("name").addContent(
					new Element("firstname")).addContent(
					new Element("lastname"));

			System.out
					.println("enter [first name] of the patient (enter to keep the current value)");
			System.out.print("["
					+ patient.getChild("name").getChild("firstname").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = patient.getChild("name").getChild("firstname")
						.getText();
			} else
				changed = true;
			name.getChild("firstname").setText(input);

			System.out
					.println("enter [last name] of the patient (enter to keep the current value)");
			System.out.print("["
					+ patient.getChild("name").getChild("lastname").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = patient.getChild("name").getChild("lastname").getText();
			} else
				changed = true;
			name.getChild("lastname").setText(input);

			newPatient.addContent(name);

			System.out
					.println("enter [age] of the patient (enter to keep the current value)");
			System.out
					.print("[" + insertInteger(publicStat, "age") + "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = publicStat.getChild("age").getText();
			} else
				changed = true;

			newPublicStat.addContent((new Element("age")).setText(input));

			System.out
					.println("enter [gender] of the patient (enter to keep the current value)");
			System.out.print("[" + publicStat.getChild("gender").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = publicStat.getChild("gender").getText();
			} else
				changed = true;
			newPublicStat.addContent(new Element("gender").setText(input));

			System.out.println("enter [height] of the patient in feet");
			System.out.println("[" + insertDouble(publicStat, "height") + "]>");
			input = stdin.readLine();
			if (input.equals("")) {
				input = publicStat.getChild("height").getText();
			} else
				changed = true;

			newPublicStat.addContent((new Element("height")).setText(input));

			System.out.println("enter [weight] of the patient in lb");
			System.out.println("[" + insertDouble(publicStat, "weight")
					+ "]>");
			input = stdin.readLine();
			if (input.equals("")) {
				input = publicStat.getChild("weight").getText();
			} else
				changed = true;
			
			newPublicStat.addContent((new Element("weight")).setText(input));

			Element contact = new Element("contact")
					.addContent(new Element("street"))
					.addContent(new Element("city"))
					.addContent(new Element("state"))
					.addContent(new Element("zip"))
					.addContent(new Element("phone"))
					.addContent(new Element("fax"))
					.addContent(new Element("email"));

			System.out
					.println("enter [street] of the patient contact (enter to keep the current value)");
			System.out.print("["
					+ patient.getChild("contact").getChild("street").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = patient.getChild("contact").getChild("street")
						.getText();
			} else
				changed = true;
			contact.getChild("street").setText(input);

			System.out
					.println("enter [city] of the patient contact (enter to keep the current value)");
			System.out.print("["
					+ patient.getChild("contact").getChild("city").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = patient.getChild("contact").getChild("city").getText();
			} else {
				changed = true;
			}
			contact.getChild("city").setText(input);
			newPublicStat.addContent((new Element("city")).setText(input));

			System.out
					.println("enter [state] of the patient contact (enter to keep the current value)");
			System.out.print("["
					+ patient.getChild("contact").getChild("state").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = patient.getChild("contact").getChild("state").getText();
			} else {
				changed = true;
			}
			contact.getChild("state").setText(input);
			newPublicStat.addContent((new Element("state")).setText(input));

			System.out
					.println("enter [zip] of the patient contact (enter to keep the current value)");
			System.out.print("["
					+ patient.getChild("contact").getChild("zip").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = patient.getChild("contact").getChild("zip").getText();
			} else
				changed = true;
			contact.getChild("zip").setText(input);

			System.out
					.println("enter [phone number] of the patient contact (enter to keep the current value)");
			System.out.print("["
					+ patient.getChild("contact").getChild("phone").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = patient.getChild("contact").getChild("phone").getText();
			} else
				changed = true;
			contact.getChild("phone").setText(input);

			System.out
					.println("enter [fax number] of the patient contact (enter to keep the current value)");
			System.out.print("["
					+ patient.getChild("contact").getChild("fax").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = patient.getChild("contact").getChild("fax").getText();
			} else
				changed = true;
			contact.getChild("fax").setText(input);

			System.out
					.println("enter [email] of the patient contact (enter to keep the current value)");
			System.out.print("["
					+ patient.getChild("contact").getChild("email").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = patient.getChild("contact").getChild("email").getText();
			} else
				changed = true;
			contact.getChild("email").setText(input);

			newPatient.addContent(contact);

			XMLOutputter outputter = new XMLOutputter(Format.getRawFormat());
			System.out.println(outputter.outputString(newPublicStat));

			
			if (changed) {
				private_root.addContent(0, newPatient);
				public_root.addContent(0, newPublicStat);
			}

			// XMLOutputter outputter = new XMLOutputter(Format.getRawFormat());
			// System.out.println(outputter.outputString(document));

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void guardianUpdate(Identity identity, BufferedReader stdin) {
		Element root = privateRecord.getRootElement();
		Element guardian = (Element) root.getChildren("guardian").get(0);

		Element newGuardian = new Element("guardian").setAttribute(
				"modifiedby", identity.id);

		String input;
		boolean changed = false;

		try {
			Element name = new Element("name").addContent(
					new Element("firstname")).addContent(
					new Element("lastname"));

			System.out
					.println("enter [first name] of the guardian (enter to keep the current value)");
			System.out.print("["
					+ guardian.getChild("name").getChild("firstname").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = guardian.getChild("name").getChild("firstname")
						.getText();
			} else
				changed = true;
			name.getChild("firstname").setText(input);

			System.out
					.println("enter [last name] of the guardian (enter to keep the current value)");
			System.out.print("["
					+ guardian.getChild("name").getChild("lastname").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = guardian.getChild("name").getChild("lastname")
						.getText();
			} else
				changed = true;
			name.getChild("lastname").setText(input);

			newGuardian.addContent(name);

			Element contact = new Element("contact")
					.addContent(new Element("street"))
					.addContent(new Element("city"))
					.addContent(new Element("state"))
					.addContent(new Element("zip"))
					.addContent(new Element("phone"))
					.addContent(new Element("fax"))
					.addContent(new Element("email"));

			System.out
					.println("enter [street] of the guardian contact (enter to keep the current value)");
			System.out.print("["
					+ guardian.getChild("contact").getChild("street").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = guardian.getChild("contact").getChild("street")
						.getText();
			} else
				changed = true;
			contact.getChild("street").setText(input);

			System.out
					.println("enter [city] of the guardian contact (enter to keep the current value)");
			System.out.print("["
					+ guardian.getChild("contact").getChild("city").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = guardian.getChild("contact").getChild("city").getText();
			} else
				changed = true;
			contact.getChild("city").setText(input);

			System.out
					.println("enter [state] of the guardian contact (enter to keep the current value)");
			System.out.print("["
					+ guardian.getChild("contact").getChild("state").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = guardian.getChild("contact").getChild("state")
						.getText();
			} else
				changed = true;
			contact.getChild("state").setText(input);

			System.out
					.println("enter [zip] of the guardian contact (enter to keep the current value)");
			System.out.print("["
					+ guardian.getChild("contact").getChild("zip").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = guardian.getChild("contact").getChild("zip").getText();
			} else
				changed = true;
			contact.getChild("zip").setText(input);

			System.out
					.println("enter [phone number] of the guardian contact (enter to keep the current value)");
			System.out.print("["
					+ guardian.getChild("contact").getChild("phone").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = guardian.getChild("contact").getChild("phone")
						.getText();
			} else
				changed = true;
			contact.getChild("phone").setText(input);

			System.out
					.println("enter [fax number] of the guardian contact (enter to keep the current value)");
			System.out.print("["
					+ guardian.getChild("contact").getChild("fax").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = guardian.getChild("contact").getChild("fax").getText();
			} else
				changed = true;
			contact.getChild("fax").setText(input);

			System.out
					.println("enter [email] of the guardian contact (enter to keep the current value)");
			System.out.print("["
					+ guardian.getChild("contact").getChild("email").getText()
					+ "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = guardian.getChild("contact").getChild("email")
						.getText();
			} else
				changed = true;
			contact.getChild("email").setText(input);

			newGuardian.addContent(contact);

			if (changed) {
				root.addContent(0, newGuardian);
			}

			// XMLOutputter outputter = new XMLOutputter(Format.getRawFormat());
			// System.out.println(outputter.outputString(privateRecord));

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void pdUpdate(Identity identity, BufferedReader stdin) {
		Element root = privateRecord.getRootElement();
		Element primarydoctor = (Element) root.getChildren("primarydoctor")
				.get(0);

		Element newPrimaryDoctor = new Element("primarydoctor").setAttribute(
				"modifiedby", identity.id);

		String input;
		boolean changed = false;

		try {
			Element name = new Element("name").addContent(
					new Element("firstname")).addContent(
					new Element("lastname"));

			System.out
					.println("enter [first name] of the primary doctor (enter to keep the current value)");
			System.out.print("["
					+ primarydoctor.getChild("name").getChild("firstname")
							.getText() + "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = primarydoctor.getChild("name").getChild("firstname")
						.getText();
			} else
				changed = true;
			name.getChild("firstname").setText(input);

			System.out
					.println("enter [last name] of the primary doctor (enter to keep the current value)");
			System.out.print("["
					+ primarydoctor.getChild("name").getChild("lastname")
							.getText() + "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = primarydoctor.getChild("name").getChild("lastname")
						.getText();
			} else
				changed = true;
			name.getChild("lastname").setText(input);

			newPrimaryDoctor.addContent(name);

			Element contact = new Element("contact")
					.addContent(new Element("street"))
					.addContent(new Element("city"))
					.addContent(new Element("state"))
					.addContent(new Element("zip"))
					.addContent(new Element("phone"))
					.addContent(new Element("fax"))
					.addContent(new Element("email"));

			System.out
					.println("enter [street] of the primary doctor contact (enter to keep the current value)");
			System.out.print("["
					+ primarydoctor.getChild("contact").getChild("street")
							.getText() + "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = primarydoctor.getChild("contact").getChild("street")
						.getText();
			} else
				changed = true;
			contact.getChild("street").setText(input);

			System.out
					.println("enter [city] of the primary doctor contact (enter to keep the current value)");
			System.out.print("["
					+ primarydoctor.getChild("contact").getChild("city")
							.getText() + "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = primarydoctor.getChild("contact").getChild("city")
						.getText();
			} else
				changed = true;
			contact.getChild("city").setText(input);

			System.out
					.println("enter [state] of the primary doctor contact (enter to keep the current value)");
			System.out.print("["
					+ primarydoctor.getChild("contact").getChild("state")
							.getText() + "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = primarydoctor.getChild("contact").getChild("state")
						.getText();
			} else
				changed = true;
			contact.getChild("state").setText(input);

			System.out
					.println("enter [zip] of the primary doctor contact (enter to keep the current value)");
			System.out.print("["
					+ primarydoctor.getChild("contact").getChild("zip")
							.getText() + "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = primarydoctor.getChild("contact").getChild("zip")
						.getText();
			} else
				changed = true;
			contact.getChild("zip").setText(input);

			System.out
					.println("enter [phone number] of the primary doctor contact (enter to keep the current value)");
			System.out.print("["
					+ primarydoctor.getChild("contact").getChild("phone")
							.getText() + "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = primarydoctor.getChild("contact").getChild("phone")
						.getText();
			} else
				changed = true;
			contact.getChild("phone").setText(input);

			System.out
					.println("enter [fax number] of the primary doctor contact (enter to keep the current value)");
			System.out.print("["
					+ primarydoctor.getChild("contact").getChild("fax")
							.getText() + "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = primarydoctor.getChild("contact").getChild("fax")
						.getText();
			} else
				changed = true;
			contact.getChild("fax").setText(input);

			System.out
					.println("enter [email] of the primary doctor contact (enter to keep the current value)");
			System.out.print("["
					+ primarydoctor.getChild("contact").getChild("email")
							.getText() + "] >");
			input = stdin.readLine();
			if (input.equals("")) {
				input = primarydoctor.getChild("contact").getChild("email")
						.getText();
			} else
				changed = true;
			contact.getChild("email").setText(input);

			newPrimaryDoctor.addContent(contact);

			if (changed) {
				root.addContent(0, newPrimaryDoctor);
			}

			// XMLOutputter outputter = new XMLOutputter(Format.getRawFormat());
			// System.out.println(outputter.outputString(document));

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void addDiagnosis(Identity identity, BufferedReader stdin) {
		Element root = publicRecord.getRootElement();

		Element newDiagnosis = new Element("diagnosis").setAttribute(
				"modifiedby", identity.id);

		String input;

		newDiagnosis.addContent(new Element("doctor")).setText(
				identity.firstName + " " + identity.lastName);
		try {
			System.out.println("helle, doctor " + identity.lastName);
			
			DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
	        Date date = new Date();
	        			
			System.out.println("enter [date] of the diagnosis [" + dateFormat.format(date) + "]");
			System.out.print(">");
			input = stdin.readLine();
			if (input.equals("")) {
		        input = dateFormat.format(date);
			}
			newDiagnosis.addContent(new Element("date").setText(input));

			System.out.println("enter [diagnosis] of the treatment");
			System.out.print(">");
			input = stdin.readLine();
			newDiagnosis.addContent(new Element("diagnosis").setText(input));

			root.getChild("medical").addContent(0, newDiagnosis);

			// XMLOutputter outputter = new XMLOutputter(Format.getRawFormat());
			// System.out.println(outputter.outputString(document));

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void addTreatment(Identity identity, BufferedReader stdin) {
		Element root = publicRecord.getRootElement();

		Element newTreatment = new Element("treatment").setAttribute(
				"modifiedby", identity.id);

		String input;

		try {
			System.out.println("enter [type] of the treatment");
			System.out.print(">");
			input = stdin.readLine();
			newTreatment.addContent(new Element("type").setText(input));

			System.out.println("enter [decription] of the treatment");
			System.out.print(">");
			input = stdin.readLine();
			newTreatment.addContent(new Element("description").setText(input));

			System.out.println("enter [comment] of the testreatment");
			System.out.print(">");
			input = stdin.readLine();
			newTreatment.addContent(new Element("comment").setText(input));

			DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
	        Date date = new Date();
	        			
			System.out.println("enter [beginning date] of the treatment [" + dateFormat.format(date) + "] type enter for defalt");
			System.out.print(">");
			input = stdin.readLine();
			if (input.equals("")) {
		        input = dateFormat.format(date);
			}
			newTreatment.addContent(new Element("date").setText(input));

			root.getChild("medical").addContent(0, newTreatment);

			// XMLOutputter outputter = new XMLOutputter(Format.getRawFormat());
			// System.out.println(outputter.outputString(document));

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void addTestResult(Identity identity, BufferedReader stdin) {
		Element root = publicRecord.getRootElement();

		Element newTestResult = new Element("testresult").setAttribute(
				"modifiedby", identity.id);

		String input;

		try {
			System.out.println("enter [name] of the test");
			System.out.print(">");
			input = stdin.readLine();
			newTestResult.addContent(new Element("test").setText(input));

			System.out.println("enter [result] of the test");
			System.out.print(">");
			input = stdin.readLine();
			newTestResult.addContent(new Element("result").setText(input));

			System.out.println("enter [comment] of the test");
			System.out.print(">");
			input = stdin.readLine();
			newTestResult.addContent(new Element("comment").setText(input));

			DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
	        Date date = new Date();
	        			
			System.out.println("enter [date] of the test [" + dateFormat.format(date) + "]");
			System.out.print(">");
			input = stdin.readLine();
			if (input.equals("")) {
		        input = dateFormat.format(date);
			}
			newTestResult.addContent(new Element("date").setText(input));

			System.out.println("enter [performer] of the test");
			System.out.print(">");
			input = stdin.readLine();
			newTestResult.addContent(new Element("performedby").setText(input));

			root.getChild("medical").addContent(0, newTestResult);

			// XMLOutputter outputter = new XMLOutputter(Format.getRawFormat());
			// System.out.println(outputter.outputString(document));

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void printHealthRecord() {

		System.out.println("MEDICAL RECORD of " + patientId);
		printPatientHistory();
		System.out.println();
		printGuardianHistory();
		System.out.println();
		printPrimaryDoctorHistory();
		System.out.println();
		printDiagnosisHistory();
		System.out.println();
		printTreatmentHistory();
		System.out.println();
		printTestResultHistory();
	}

	private void printPatientHistory() {
		@SuppressWarnings("unchecked")
		List<Element> privatelist = privateRecord.getRootElement().getChildren(
				"patient");
		@SuppressWarnings("unchecked")
		List<Element> publiclist = publicRecord.getRootElement().getChildren(
				"publicstat");

		for (int index = 0; index < publiclist.size() - 1; index++) {
			Element privateHistory = privatelist.get(index);
			Element publicHistory = publiclist.get(index);
			if (index == 0)
				System.out.println("patient information - modifier: "
						+ privateHistory.getAttributeValue("modifiedby"));
			else
				System.out.println("patient information history - modifier: "
						+ privateHistory.getAttributeValue("modifiedby"));
			System.out.println(" name : "
					+ privateHistory.getChild("name").getChildText("firstname")
					+ " "
					+ privateHistory.getChild("name").getChildText("lastname"));

			System.out.println(" age: " + publicHistory.getChildText("age"));
			System.out.println(" gender: "
					+ publicHistory.getChildText("gender"));

			if (publicHistory != null) {
				if (publicHistory.getChild("height") != null) {
					System.out.println(" height: "
							+ publicHistory.getChild("height").getText());
					System.out.println(" weight: "
							+ publicHistory.getChild("weight").getText());
				} else
					System.out.println("height is null");

			} else
				System.out.println("public stat is null");

			System.out.println(" contact");
			System.out.println(" address: "
					+ privateHistory.getChild("contact").getChildText("street")
					+ ",");
			System.out.println("          "
					+ privateHistory.getChild("contact").getChildText("city")
					+ ", "
					+ privateHistory.getChild("contact").getChildText("state")
					+ " "
					+ privateHistory.getChild("contact").getChildText("zip"));
			System.out.println(" phone: "
					+ privateHistory.getChild("contact").getChildText("phone"));
			System.out.println(" fax: "
					+ privateHistory.getChild("contact").getChildText("fax"));
			System.out.println(" email: "
					+ privateHistory.getChild("contact").getChildText("email"));
		}
	}

	private void printGuardianHistory() {
		@SuppressWarnings("unchecked")
		List<Element> list = privateRecord.getRootElement().getChildren(
				"guardian");

		for (int index = 0; index < list.size() - 1; index++) {
			Element history = list.get(index);
			if (index == 0)
				System.out.println("guardian information - modifier: "
						+ history.getAttributeValue("modifiedby"));
			else
				System.out.println("guardian information history - modifier: "
						+ history.getAttributeValue("modifiedby"));
			System.out.println(" name : "
					+ history.getChild("name").getChildText("firstname") + " "
					+ history.getChild("name").getChildText("lastname"));
			System.out.println(" contact");
			System.out.println(" address: "
					+ history.getChild("contact").getChildText("street") + ",");
			System.out.println("          "
					+ history.getChild("contact").getChildText("city") + ", "
					+ history.getChild("contact").getChildText("state") + " "
					+ history.getChild("contact").getChildText("zip"));
			System.out.println(" phone: "
					+ history.getChild("contact").getChildText("phone"));
			System.out.println(" fax: "
					+ history.getChild("contact").getChildText("fax"));
			System.out.println(" email: "
					+ history.getChild("contact").getChildText("email"));

		}
	}

	private void printPrimaryDoctorHistory() {
		@SuppressWarnings("unchecked")
		List<Element> list = privateRecord.getRootElement().getChildren(
				"primarydoctor");

		for (int index = 0; index < list.size() - 1; index++) {
			Element history = list.get(index);
			if (index == 0)
				System.out.println("primary doctor information - modifier: "
						+ history.getAttributeValue("modifiedby"));
			else
				System.out
						.println("primary doctor information history - modifier: "
								+ history.getAttributeValue("modifiedby"));
			System.out.println(" name : "
					+ history.getChild("name").getChildText("firstname") + " "
					+ history.getChild("name").getChildText("lastname"));
			System.out.println(" contact");
			System.out.println(" address: "
					+ history.getChild("contact").getChildText("street") + ",");
			System.out.println("          "
					+ history.getChild("contact").getChildText("city") + ", "
					+ history.getChild("contact").getChildText("state") + " "
					+ history.getChild("contact").getChildText("zip"));
			System.out.println(" phone: "
					+ history.getChild("contact").getChildText("phone"));
			System.out.println(" fax: "
					+ history.getChild("contact").getChildText("fax"));
			System.out.println(" email: "
					+ history.getChild("contact").getChildText("email"));
		}
	}

	private void printDiagnosisHistory() {
		@SuppressWarnings("unchecked")
		List<Element> list = publicRecord.getRootElement().getChild("medical")
				.getChildren("diagnosis");

		if (list.size() > 1) {
			for (int index = 0; index < (list.size() - 1) ; index++) {
				Element history = list.get(index);
				System.out.println("Diagnosis - modifier: "
						+ history.getAttributeValue("modifiedby"));
				System.out.println(" diagnosis: "
						+ history.getChildText("diagnosis"));
				System.out.println(" date: " + history.getChildText("date"));
			}
		} else
			System.out.println("no diagnosis made");
	}

	private void printTreatmentHistory() {
		@SuppressWarnings("unchecked")
		List<Element> list = publicRecord.getRootElement().getChild("medical")
				.getChildren("treatment");
		if (list.size() > 1) {
			for (int index = 0; index < (list.size() - 1) ; index++) {
				Element history = list.get(index);
				System.out.println("treatment - modifier: "
						+ history.getAttributeValue("modifiedby"));
				System.out.println(" type: " + history.getChildText("type"));
				System.out.println(" description: "
						+ history.getChildText("description"));
				System.out.println(" comment: "
						+ history.getChildText("comment"));
				System.out.println(" date: " + history.getChildText("date"));
			}
		} else
			System.out.println("no treatment");
	}

	private void printTestResultHistory() {
		@SuppressWarnings("unchecked")
		List<Element> list = publicRecord.getRootElement().getChild("medical")
				.getChildren("testresult");

		if (list.size() > 1) {
			for (int index = 0; index < (list.size() - 1) ; index++) {
				Element history = list.get(index);
				System.out.println("test result - modifier: "
						+ history.getAttributeValue("modifiedby"));
				System.out.println(" name: " + history.getChildText("test"));
				System.out
						.println(" result: " + history.getChildText("result"));
				System.out.println(" comment: "
						+ history.getChildText("comment"));
				System.out.println(" date: " + history.getChildText("type"));
				System.out.println(" performed by: "
						+ history.getChildText("performedby"));
			}
		} else
			System.out.println("no test result");
	}

	public String getId() {
		return patientId;
	}
}
